package com.android.sms;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.sms.interface_.OkHttpCallBack;
import com.android.sms.manager.API_Manager;
import com.android.sms.responce.BaseResponce;
import com.android.sms.util.SPUtil;

public class MainActivity extends AppCompatActivity {

    IntentFilter filter;
    SmsReceiver receiver;

    TextView tv_text, tv_setting;
    TextView tv_phone, tv_host;
    int count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_text = findViewById(R.id.tv_text);
        tv_setting = findViewById(R.id.tv_setting);
        tv_phone = findViewById(R.id.tv_phone);
        tv_host = findViewById(R.id.tv_host);

        tv_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, SettingActivity.class));
            }
        });


        filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        receiver = new SmsReceiver();
        registerReceiver(receiver, filter);//注册广播接收器
    }

    @Override
    protected void onResume() {
        super.onResume();

        String phone = SPUtil.getStringValue(MainActivity.this, Constents.sp_phone, "");
        String host = SPUtil.getStringValue(MainActivity.this, Constents.sp_host, "");

        if (!TextUtils.isEmpty(phone)) {
            tv_phone.setText("手机号：" + phone);
        } else {
            tv_phone.setText("手机号：未配置");
        }

        if (!TextUtils.isEmpty(host)) {
            tv_host.setText("服务器：" + host);
        } else {
            tv_host.setText("服务器：未配置");
        }
    }

    public class SmsReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            count++;
            String sender = null;//存储短信发送方手机号
            String smscontent = null;//存储短信发送方手机号
            Bundle bundle = intent.getExtras();//通过getExtras()方法获取短信内容
            String format = intent.getStringExtra("format");
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");//根据pdus关键字获取短信字节数组，数组内的每个元素都是一条短信
                for (Object object : pdus) {
                    SmsMessage message = SmsMessage.createFromPdu((byte[]) object, format);//将字节数组转化为Message对象
                    sender = message.getOriginatingAddress();//获取短信手机号
                    smscontent = message.getMessageBody();  //获取短信内容
                    Log.i("test", "sender:" + sender + "  smscontent:" + smscontent);
                    String text = smscontent;
                    tv_text.setText("累计收到短信数量：" + count);

                    sendContent(smscontent);
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);//解绑广播接收器
    }

    private void sendContent(String content) {
        API_Manager.sendContent(MainActivity.this, content, new OkHttpCallBack() {
            @Override
            public void onSuccess(BaseResponce baseResponce) {

            }

            @Override
            public void onFailure(BaseResponce baseResponce) {

            }
        });
    }

}